package com.yx.cloud.activity;

import android.view.View;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.yx.cloud.R;
import com.yx.cloud.adapter.MusicTitleTabAdapter;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.bean.MusicTabBean;
import com.yx.cloud.databinding.MuscicatelistActivityBinding;
import com.yx.cloud.fragment.MusicAlbumListFragment;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.view.MusicViewPager;
import java.util.ArrayList;
import java.util.List;

public class MusicCateListActivity extends BaseActivity implements View.OnClickListener, MusicTitleTabAdapter.InnerItemOnclickListener{
    private MuscicatelistActivityBinding binding;
    private MusicTitleTabAdapter musicTitleTabAdapter;
    private List<MusicTabBean> musicType;
    private List<MusicAlbumListFragment> fragmentList;
    private FragmentManager fm;
    private MusicViewPager musicViewPager;
    private RecyclerView recyclerView;

    @Override
    public void onCreate() {
        binding = MuscicatelistActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
        initView(binding.getRoot());
        initViewPager();
    }

    private void initView(View view){
        //设置线性布局管理器类型
        recyclerView = view.findViewById(R.id.rv_music_title_tab_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        musicTitleTabAdapter = new MusicTitleTabAdapter(this, musicType);
        musicTitleTabAdapter.setOnInnerItemOnClickListener(this);
        recyclerView.setAdapter(musicTitleTabAdapter);
        musicViewPager = view.findViewById(R.id.vp_music);
        binding.ivMusicCateListReturn.setOnClickListener(this);
        binding.etSearch.setFocusable(false);
        binding.etSearch.setOnClickListener(this);
    }

    private void initData(){
        //TODO 获取音乐列表tab类型
        musicType = new ArrayList<>();
        MusicTabBean musicTabBean0 = new MusicTabBean();
        musicTabBean0.setCateId(10000L);
        musicTabBean0.setMusicTopicName(getResources().getString(R.string.M_Recommend));
        musicType.add(musicTabBean0);
        MusicTabBean musicTabBean2 = new MusicTabBean();
        musicTabBean2.setCateId(80021L);
        musicTabBean2.setMusicTopicName(getResources().getString(R.string.M_Music));
        musicType.add(musicTabBean2);
        MusicTabBean musicTabBean3 = new MusicTabBean();
        musicTabBean3.setCateId(80011L);
        musicTabBean3.setMusicTopicName(getResources().getString(R.string.M_Child));
        musicType.add(musicTabBean3);
        MusicTabBean musicTabBean4 = new MusicTabBean();
        musicTabBean4.setCateId(80050L);
        musicTabBean4.setMusicTopicName(getResources().getString(R.string.M_AudioBook));
        musicType.add(musicTabBean4);
        MusicTabBean musicTabBean5 = new MusicTabBean();
        musicTabBean5.setCateId(80010L);
        musicTabBean5.setMusicTopicName(getResources().getString(R.string.M_News));
        musicType.add(musicTabBean5);
        MusicTabBean musicTabBean6 = new MusicTabBean();
        musicTabBean6.setCateId(80035L);
        musicTabBean6.setMusicTopicName(getResources().getString(R.string.M_Novel));
        musicType.add(musicTabBean6);
        MusicTabBean musicTabBean1 = new MusicTabBean();
        musicTabBean1.setCateId(80054L);
        musicTabBean1.setMusicTopicName(getResources().getString(R.string.M_Humanities));
        musicType.add(musicTabBean1);
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        for(int i = 0;i < musicType.size();i++){
            MusicAlbumListFragment musicAlbumListFragment = new MusicAlbumListFragment(musicType.get(i).getCateId(),"ALBUM");
            fragmentList.add(musicAlbumListFragment);
        }

        fm = getSupportFragmentManager();
        FragAdapter adapter = new FragAdapter(fm, fragmentList);
        musicViewPager.setAdapter(adapter);
        musicViewPager.setOffscreenPageLimit(6);
        musicViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedTab(position);

                //横向recycleView中item居中显示
                int firstPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
                int lastPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));
                View view1 = recyclerView.getChildAt(position - firstPosition);
                View view2 = recyclerView.getChildAt(lastPosition - position);
                if(view1 == null || view2 == null){
                    return;
                }else {
                    int left = recyclerView.getChildAt(position - firstPosition).getLeft();
                    int right = recyclerView.getChildAt(lastPosition - position).getLeft();
                    recyclerView.scrollBy((left - right) / 2,0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id== R.id.iv_musicCateListReturn) {
            Intent_utils.backIntentFromTop(this);
        }else if(id == R.id.et_search){
            Intent_utils.enterIntentFromBottomQuickly(MusicCateListActivity.this,MusicSearchResultListActivity.class);
        }
    }

    private void selectedTab(int position){
        musicTitleTabAdapter.selectItem(position);
    }

    @Override
    public void itemClick(View v, int position) {
        //TODO 选中tab高亮显示，并且切换相应音乐列表
        musicTitleTabAdapter.notifyDataSetChanged();
        musicViewPager.setCurrentItem(position);
    }

    public class FragAdapter extends FragmentPagerAdapter {

        private List<MusicAlbumListFragment> mFragments;

        public FragAdapter(FragmentManager fm, List<MusicAlbumListFragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public MusicAlbumListFragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }
}
